var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../utils/user.js');
import {USER_PAGE} from "../../../config/common"
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
      userInfo: {
        nickName: '点击登录',
        avatarUrl: '/static/images/avatar.png'
      },
      order: {
        unpaid: 0,
        unship: 0,
        unrecv: 0,
        uncomment: 0
      },
    MyMenus: [
      { url: "/pages/ucenter/collect/collect", pic: "icon_collect.png",name:"商品收藏"},
      { url: "/pages/ucenter/footprint/footprint", pic: "footprint.png", name: "浏览足迹" },
      { url: "/pages/groupon/myGroupon/myGroupon", pic: "group.png", name: "我的拼团" },
      { url: "/pages/ucenter/address/address", pic: "address.png", name: "地址管理" },
      { url: "/pages/ucenter/feedback/feedback", pic: "feedback.png", name: "意见反馈" },
    //   { url: "/pages/about/about", pic: "about_us.png", name: "关于我们" }
      // *,{ url: "/pages/about/about", pic: "comment.png", name: "使用帮助" }
      ],
      AdminMenus: [
        { url: "/pages/admin/goods/goods", pic:"goods.png",name:"商品管理"},
        { url: "/pages/admin/orders/orderlist/orderlist", pic: "order2.png", name: "订单管理" },
        { url: "/pages/admin/members/members", pic: "member.png", name: "会员管理" },
        { url: "/pages/admin/stores/list/list", pic: "stores.png", name: "店铺管理" },
        { url: "/pages/distribution/manage/list", pic: "distribution.png", name: "分销管理" },
        { url: "/pages/admin/coupons/coupons", pic: "coupon.png", name: "卡券管理" },
        { url: "/pages/admin/notice/notice", pic: "circulars.png", name: "消息通知管理" },
        //{ url: "/pages/Commission/manage/list", pic: "group.png", name: "佣金管理" },
        ],
      hasLogin: false,
      // hasLogin: true,
      totalAmount: 0.00,
      
  },

  /**
   * 页面跳转
  */
  goPages:function(e){
      //检查过期卡券
      
    if (this.data.hasLogin) {
        util.request(api.CouponPast, {
        }).then(function(res) {
            //修改状态成功
            // 刷新页面
            wx.navigateTo({
                url: e.currentTarget.dataset.url
              });
        })
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },
  onShow:function(){
    if(this.data.userInfo.nickName == "点击登录"){
      this.data.hasLogin = false;
    }
    //获取用户的登录信息
    if (app.globalData.hasLogin) {
      console.log(app.globalData.user)
      this.setData({
        userInfo:app.globalData.user,
        hasLogin:true
      })
      console.log(this.data.userInfo)
      console.log(this.data.hasLogin)
      // let userInfo = wx.getStorageSync('userInfo');
      // console.log(1111)
      // console.log(userInfo);
      // this.setData({
      //   userInfo: userInfo,
      //   hasLogin: true
      // });
      
      // let that = this;
      // util.request(api.UserIndex).then(function (res) {
      //   if (res.errno === 0) {
      //     that.setData({
      //       order: res.data.order,
      //       totalAmount: res.data.totalAmount,
      //       remainAmount: res.data.remainAmount,
      //       couponCount: res.data.couponCount
      //     });
      //   }
      //   console.log(res.data.order)
      //   console.log(res.data.totalAmount)
      //   console.log(res.data.remainAmount)
      //   console.log(res.data.couponCount)
      // });
      // console.log(this.data.order)
    }
    
     
    //检查当面是否有自定义的tabBar
    if (typeof this.getTabBar === 'function' &&  this.getTabBar()) {
      let roldId = wx.getStorageSync('roldId');
      if(roldId == 0){
        this.getTabBar().setData({
          selected:2,
          list: USER_PAGE.memberTabbarList
        })
      }else{
        this.getTabBar().setData({
          selected:0,
          list: USER_PAGE.adminTabbarList
        })
      }
    }
  },

  /**
  * 生命周期函数--监听页面卸载
  */
  onUnload: function () {

  },
  goLogin() {

    this.data.hasLogin =true;
    if (this.data.hasLogin) {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },
  goBrokerage() {
    if (this.data.hasLogin) {
      wx.navigateTo({
        url: "/pages/brokerage/main/main"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },
  goOrder() {
    if (this.data.hasLogin) {
      try {
        wx.setStorageSync('tab', '0');
      } catch (e) {

      }
      wx.navigateTo({
        url: "/pages/ucenter/order/order"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },
  goOrderIndex(e) {
    if (this.data.hasLogin) {
      let tab = e.currentTarget.dataset.index
      let route = e.currentTarget.dataset.route
      try {
        wx.setStorageSync('tab', tab);
      } catch (e) {

      }
      wx.navigateTo({
        url: route,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },
  goAfterSale: function () {
    wx.showToast({
      title: '目前不支持',
      icon: 'none',
      duration: 2000
    });
  },

  //调用微信扫码功能
  scanCode: function() {
    if(this.data.hasLogin){
      wx.scanCode({
        success: function(res) {
          // 扫码成功后
          let info = res.result; // 假设你需要传递的信息是扫码结果
          wx.setStorageSync('info', info); // 将数据存储在缓存中
          wx.navigateTo({
          url: '/pages/remove/remove'
          });
        },
        fail: function(res) {
          console.log(res);
        }
      })
    }else{
      wx.navigateTo({
        url: '/pages/auth/login/login',
      })
    }
   
  }
})